<?php

namespace App\Admin\Controllers\Partner;

use App\Admin\Forms\Partner\PartnerMiningForm;
use App\Admin\Forms\Partner\PartnerUserForm;
use App\Admin\Repositories\Partner\PartnerUser;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\Widgets\Modal;

class PartnerUserController extends AdminController
{

    public function index(Content $content)
    {
        return $content
            ->title('合伙人会员')
            ->description($this->description()['index'] ?? trans('admin.list'))
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new PartnerUser(['partner','user']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('partner_id','等级号');
            $grid->column('partner.name','合伙人名称');
            $grid->column('user_id','会员号');
            $grid->column("user.username",'会员联系方式');
            $grid->column('status','状态')->display(function ($value){
                if($value == 1){
                    return "启用";
                }else{
                    return "禁用";
                }
            });

            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->model()->orderBy('id','desc');
            $grid->disableEditButton();
            $grid->disableCreateButton();
            $grid->disableBatchDelete();
            $grid->disableDeleteButton();
            $grid->actions(function (Grid\Displayers\Actions $action){

                if(Admin::user()->can("/partner/partner_user/show")) {
                    $action->append(Modal::make()
                        ->lg()
                        ->title("合伙人会员等级调整")
                        ->body(PartnerUserForm::make()->payload(['id'=>$this->id]))
                        ->button("等级调整"));
                }
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal("user_id",'会员号');
                $filter->like("user.username",'会员联系方式');
                $filter->equal("partner_id",'合伙人等级');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new PartnerUser(), function (Show $show) {
            $show->field('id');
            $show->field('partner_id');
            $show->field('status');
            $show->field('user_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new PartnerUser(), function (Form $form) {
            $form->display('id');
            $form->text('partner_id');
            $form->text('status');
            $form->text('user_id');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
